package cn.glinks.liuhe.activity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.EditText;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;
import cn.glinks.liuhe.R;
import cn.glinks.liuhe.api.ApiConstants;
import cn.glinks.liuhe.api.ApiRequest;
import cn.glinks.liuhe.base.BaseActivity;
import cn.glinks.liuhe.baserx.RxSubscriber;
import cn.glinks.liuhe.utils.ActTransAnimUtils;
import cn.glinks.liuhe.utils.FormatUtils;
import cn.glinks.liuhe.utils.GsonUtils;
import cn.glinks.liuhe.utils.SmscodeCountDownTimerUtils;
import cn.glinks.liuhe.utils.ToastUtils;

/**
 * Des:忘记密码页面
 *
 * @author MarsBlade of Cyh
 * @created 2017-03-21 14:43
 */
public class ForgetPasswdActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView mTitleText;

    @BindView(R.id.tv_smscode)
    TextView mSmscodeText;

    @BindView(R.id.et_phone)
    EditText mPhoneEdit;

    @BindView(R.id.et_smscode)
    EditText mSmscodeEdit;

    @BindView(R.id.et_passwd)
    EditText mPasswdEdit;

    // 变量
    private String mPhoneStr;
    private String mSmscodeStr;
    private String mPasswdStr;

    @Override
    public int getLayoutId() {
        return R.layout.activity_forget_passwd;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        // 设置为透明状态栏
        SetTranslanteBar();
        // 初始化标题
        mTitleText.setText("忘记密码");
    }

    /*--------------------------------------------------------------------------------
     *                          点击事件接口实现
     ---------------------------------------------------------------------------------*/
    // 获取验证码
    @OnClick(R.id.tv_smscode)
    public void clickGetSmscode() {
        // 获取数据
        mPhoneStr = mPhoneEdit.getText().toString().trim();
        // 检查数据有效性
        if (mPhoneStr.isEmpty() || !FormatUtils.isMobileNO(mPhoneStr)) {
            ToastUtils.showShort("请输入正确的手机号");
            return;
        }
        // 发送获取验证码请求
        mRxManager.add(ApiRequest.requestGetSmscode(mPhoneStr, 2)
                .subscribe(new RxSubscriber<String>(mContext) {
                    @Override
                    protected void _onNext(String msg) {
                        // 响应处理
                        if (GsonUtils.getIntValue(msg, "error") == 0) {
                            ToastUtils.showShort("获取验证码成功");
                            // 启动验证码按钮倒计时动作
                            SmscodeCountDownTimerUtils mCountDownTimerUtils = new SmscodeCountDownTimerUtils(mSmscodeText, 90000, 1000);
                            mCountDownTimerUtils.start();
                        } else {
                            String reason = GsonUtils.getValue(msg, "reason");
                            ApiConstants.showError(reason);
                        }
                    }

                    @Override
                    protected void _onError(String message) {
                        // TODO:处理错误响应
                        ToastUtils.showShort("获取验证码失败:"+message);
                    }
                }));
    }

    // 修改完成
    @OnClick(R.id.btn_next)
    public void clickNext() {
        // 获取数据
        mPhoneStr = mPhoneEdit.getText().toString().trim();
        mSmscodeStr = mSmscodeEdit.getText().toString().trim();
        mPasswdStr = mPasswdEdit.getText().toString().trim();
        // 检查数据有效性
        if (mPhoneStr.isEmpty() || !FormatUtils.isMobileNO(mPhoneStr)) {
            ToastUtils.showShort("请输入正确的手机号");
            return;
        }
        if ((mPasswdStr.length() < 6) || (mPasswdStr.length() > 16)) {
            ToastUtils.showShort("密码长度范围6~16");
            return;
        }
        if (mSmscodeStr.isEmpty()){
            ToastUtils.showShort("请输入验证码");
            return;
        }
        // 发送网络请求
        mRxManager.add(ApiRequest.requestResetPasswd(mPhoneStr, mSmscodeStr, mPasswdStr)
                .subscribe(new RxSubscriber<String>(mContext) {
                    @Override
                    protected void _onNext(String msg) {
                        int errorCode = GsonUtils.getIntValue(msg, "error");
                        String reason = GsonUtils.getValue(msg, "reason");
                        // 响应处理
                        if (errorCode == 0) {
                            ToastUtils.showShort("密码修改成功");
                            // 返回登录页面
                            ActTransAnimUtils.finishActionSlideLeftToRight(ForgetPasswdActivity.this);
                        } else if (errorCode == 3) {
                            ToastUtils.showShort(reason);
                        } else {
                            ApiConstants.showError(reason);
                        }
                    }

                    @Override
                    protected void _onError(String message) {
                        // TODO:处理失败响应
                        ToastUtils.showShort("密码修改失败:"+message);
                    }
                }));
    }

    /**
     * 监听主页键,返回键
     *
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            // 返回登录页面
            ActTransAnimUtils.finishActionSlideLeftToRight(ForgetPasswdActivity.this);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
